package team.glh.demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:Gan Linghao
 * @date:2020/4/24
 * @time:8:51
 * @description:从多个xml文件中提取数据，然后一次性批量插入表格
 */
public class Index {

    public static void main(String[] args){

        //有9682个xml文件，需要提取数据并把相关数据插入表格
        int num =9682;

        for (int i =1;i<=num;i++){

            XmlToExcelUtil3 xmlToExcelUtil3 = new XmlToExcelUtil3();

            //需要插入表格的数据
            List<List<Object>> devs = xmlToExcelUtil3.toExcel(i,"D:/软件下载/xml/down/exportDeviceTemplate_typeid="+i+".xml");

            if(i == num){

                String pathName = "E:/oid.xlsx";
                String sheetName = "oid表";

                //EasyExcel写法
                EasyExcel.write(pathName).head(head()).sheet(sheetName).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).doWrite(devs);
            }
        }
    }

    //设置表头
    private static List<List<String>> head(){
        ArrayList<List<String>> objects = new ArrayList<>();

        List<String> head0 = new ArrayList<>();
        head0.add("oid");
        head0.add("编号");

        List<String> head1 = new ArrayList<>();
        head1.add("oid");
        head1.add("OID");

        List<String> head2 = new ArrayList<>();
        head2.add("oid");
        head2.add("VendorName");

        List<String> head3 = new ArrayList<>();
        head3.add("oid");
        head3.add("CategoryName");

        List<String> head4 = new ArrayList<>();
        head4.add("oid");
        head4.add("DeviceType");

        objects.add(head0);
        objects.add(head1);
        objects.add(head2);
        objects.add(head3);
        objects.add(head4);

        return objects;

    }

}
